package com.pocket.cloud.order.controller;

import com.pocket.cloud.common.api.ResultData;
import com.pocket.cloud.common.client.PaymentClient;
import com.pocket.cloud.common.dto.OrderSettlementDTO;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhaozhile
 */
@RestController
public class OrderController {

    private static final Logger LOGGER = LoggerFactory.getLogger(OrderController.class);

    @Resource
    private PaymentClient paymentClient;
    @Resource
    private DiscoveryClient discoveryClient;;

    @GetMapping(value = "/order/payment/{orderNo}")
    public ResultData<OrderSettlementDTO> getPayByOrderNo(@PathVariable("orderNo") String orderNo)
    {
        printAllService();
        LOGGER.info("查询 order 服务，获取订单[{}]详情。", orderNo);
        // 【cloud-openfeign-1.5】
        return paymentClient.getPayByOrderNo(orderNo);
    }

    // 【cloud-loadbalancer-2】
    private void printAllService()
    {
        discoveryClient.getServices().forEach(serviceName -> {
            discoveryClient.getInstances(serviceName).forEach(instance -> {
                LOGGER.info("可用服务 {} : {} : {} : {}", instance.getServiceId(), instance.getHost(), instance.getPort(), instance.getUri());
            });
        });
    }

}
